package com.sn.springboot_netty.utils;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * @Author fyy
 * @Description
 * @Date 2020/6/10 11:28
 */
public class ImgYsUtil {

    //测试原图
    private static File fromPic = new File("F:\\tupian\\test.jpg");

    //水印
    private static File waterPic = new File("F:\\869074038846502_20200610140742_0A_0_0_res.jpg");

    /**
     * 压缩图片
     * @throws Exception
     */
    public static void reduceImage() throws Exception{
        File toPic = new File("F:\\tupian\\test_ys_0.15.jpg");
        Thumbnails.of(fromPic).scale(1f).outputQuality(0.15f).toFile(toPic);
    }

    /**
     * 修改图片大小:遵循原图高宽比例
     * @throws Exception
     */
    public static void modifyImageSize(int width,int height) throws Exception{
        File toPic = new File("F:\\20150327232556186_"+ width + "x" +height + ".jpg");
        Thumbnails.of(fromPic).size(width,height).toFile(toPic);
    }

    /**
     * 修改图片大小:不遵循原图高宽比例
     * @throws Exception
     */
    public static void modifyImageSizeKeepAspect(int width,int height) throws Exception{
        File toPic = new File("F:\\20150327232556186_keep_"+ width + "x" +height + ".jpg");
        Thumbnails.of(fromPic).size(width,height).keepAspectRatio(false).toFile(toPic);
    }


    /**
     * 按照指定比例进行缩小和放大
     * @throws Exception
     */
    public static void modifyImageSizeByScale(Double scale) throws Exception{
        File toPic = new File("F:\\20150327232556186_scale"+ scale + ".jpg");
        Thumbnails.of(fromPic).scale(scale).toFile(toPic);
    }

    /**
     * 旋转图片
     * @param direction 角度 正数正向转，负数逆向转
     * @throws Exception
     */
    public static void modifyImageDirection(int width,int height,double direction) throws Exception{
        File toPic = new File("F:\\20150327232556186_direction"+ direction + ".jpg");
        Thumbnails.of(fromPic).size(width,height).rotate(direction).toFile(toPic);
    }

    /**
     * 图片加水印
     * @throws Exception
     */
    public static void modifyImageAddSignet(int width,int height) throws Exception{
        File toPic = new File("F:\\20150327232556186_Signet.jpg");
        Thumbnails.of(fromPic)
                .size(width,height)
                .watermark(Positions.TOP_LEFT, ImageIO.read(waterPic),0.5f)
                .outputQuality(1f)
                .toFile(toPic);
    }

    /**
     * 图片裁剪
     * @throws Exception
     */
    public static void catImage(int width,int height) throws Exception{
        File toPic = new File("F:\\20150327232556186_cat.jpg");
//        Thumbnails.of(fromPic)
//                .sourceRegion(Positions.CENTER,width,height)
//                .size(width,height)
//                .outputQuality(1f)
//                .toFile(toPic);
        Thumbnails.of(fromPic)
                .sourceRegion(0,0,width,height)
                .size(width,height)
                .outputQuality(1f)
                .toFile(toPic);
    }

    /**
     * 转换图片格式
     * @throws Exception
     */
    public static void modifyImageFormat() throws Exception{
        File toPic = new File("F:\\20150327232556186_Format.png");
        Thumbnails.of(fromPic)
                .scale(1f)
                .outputFormat("png")
                .outputQuality(1f)
                .toFile(toPic);
    }

    /**
     * 输出成文件流OutputStream
     * @throws Exception
     */
    public static void modifyImageStream() throws Exception{
        File toPic = new File("F:\\20150327232556186_stream.png");
        OutputStream os=new FileOutputStream(toPic);
        Thumbnails.of(fromPic).scale(1f).toOutputStream(os);
    }


    public static void main(String[] args) throws Exception {
        reduceImage(); //压缩图片

//        modifyImageSize(200,200); //修改图片大小:遵循原图高宽比例
//
//        modifyImageSizeByScale(0.5); //按比例放大缩小
//        modifyImageSizeByScale(2.0); //按比例放大缩小
//
//        modifyImageSizeKeepAspect(200,200); //修改图片大小:不遵循原图高宽比例
//
//        modifyImageDirection(200,200,90); //旋转图片
//
//        modifyImageAddSignet(1024,200); //图片加水印
//
//        catImage(200,200); //图片裁剪
//
//        modifyImageFormat(); //转换图片格式
//
//        modifyImageStream();
    }

}
